<template>
	<view class="wrap">
		<view class="content">
			<input class="u-border-bottom" type="text" v-model="username" placeholder="用户名" />
			<input class="u-border-bottom" type="text" v-model="password" placeholder="密码" />
			<button @tap="submit" :style="[inputStyle]" class="getCaptcha">登录</button>
			<view class="alternative">
				<view class="password">注册</view>
				<view class="issue">找回密码</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				username: '',
				password: ''
			}
		},
		computed: {
			inputStyle() {
				let style = {};
				if (this.username && this.password) {
					style.color = "#fff";
					style.backgroundColor = this.$u.color['warning'];
				}
				return style;
			}
		},
		methods: {
			async submit() {
				if (this.$u.test.isEmpty(this.username) || this.$u.test.isEmpty(this.password)) {
					this.$u.toast("用户名或密码不能为空");
					return;
				}
				const param = {
					username: this.username,
					password: this.password
				}

				const res = this.$u.api.login(param);
				if (res) {
					this.$u.toast('登录成功');
					//保存新token
					this.$u.vuex('vuex_accessToken', res.accessToken);
					this.$u.vuex('vuex_refreshToken', res.refreshToken);
					this.$u.vuex('vuex_user', {
						name: this.username,
						id: res.user
					});
					this.$u.route("/pages/index/index")
				}
			}
		}
	};
</script>

<style lang="scss" scoped>
	.wrap {
		font-size: 28rpx;
		.content {
			width: 600rpx;
			margin: 80rpx auto 0;
			input {
				text-align: left;
				margin-bottom: 40rpx;
				padding-bottom: 6rpx;
			}

			.getCaptcha {
				background-color: rgb(253, 243, 208);
				color: $u-tips-color;
				border: none;
				font-size: 30rpx;
				padding: 12rpx 0;
				&::after {
					border: none;
				}
			}
			.alternative {
				color: $u-tips-color;
				display: flex;
				justify-content: space-between;
				margin-top: 30rpx;
			}
		}

	}
</style>
